package com.gome.axwl.aoc.openApi.api.app;

import com.gome.axwl.aoc.common.util.BeanJsonUtil;
import com.gome.axwl.aoc.common.util.DateUtil;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationDto;
import com.gome.axwl.aoc.entity.dto.DriverIntegrationParamDto;
import com.gome.axwl.aoc.entity.dto.FavourTargetDto;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.facade.DriverIntegrationFacade;
import com.gome.axwl.aoc.service.DriverIntegrationService;
import com.gome.common.utils.StringUtil;
import com.gome.commons.core.ErrorCode;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/** 司机积分信息 微服务
 * @Description:
 * @Auther: yinyuxin@gome.com.cn
 * @Date: 2018/5/16 10:51
 */
@Api(tags="运营平台 司机服务质量")
@RestController
@RequestMapping(value = "/driverIntegration")
public class DriverIntegrationController {

    public static final Logger LOGGER=LoggerFactory.getLogger(DriverIntegrationController.class);

    @Autowired
    private DriverIntegrationFacade driverIntegrationFacade;
    @Autowired
    private DriverIntegrationService driverIntegrationService;

    /**
     *  查询司机服务质量
     * @param carrierNo
     * @param driverNo
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "queryDriversIntegration",method = RequestMethod.GET)
    @ApiOperation(value = "查询司机服务质量",notes = "development by yinyuxin")
        public ResultData<PageResultDto<DriverIntegrationDto>> queryDriversIntegration(@RequestParam(name = "carrierNo",required = false) String carrierNo,@RequestParam(name = "driverNo",required = false) String driverNo,@RequestParam(name = "date") String date,
                                                                                   @RequestParam(name = "pageNum",required = false) Integer pageNum, @RequestParam(name = "pageSize",required = false)  Integer pageSize){
        if (null==pageNum){
            pageNum=1;
        }
        if (null==pageSize){
            pageSize=10;
        }
        DriverIntegrationParamDto driverIntegrationParamDto=new DriverIntegrationParamDto();
        driverIntegrationParamDto.setProviderCode(carrierNo);
        driverIntegrationParamDto.setDriverCode(driverNo);
        driverIntegrationParamDto.setPageNum(null==pageNum?1:pageNum);
        driverIntegrationParamDto.setPageSize(null==pageSize?10:pageSize);
        driverIntegrationParamDto.setIntegrationDate(date);
        LOGGER.info("微服务 driverIntegration-->queryDriversIntegration()-->param:{}",BeanJsonUtil.obj2String(driverIntegrationParamDto));
        ResultData<PageResultDto<DriverIntegrationDto>> resultData = driverIntegrationFacade.queryAllDriverIntegration(driverIntegrationParamDto);
        LOGGER.info("微服务 driverIntegration-->queryDriversIntegration()-->result:{}",BeanJsonUtil.obj2String(resultData));
        return resultData;
    }

    /**
     *  计算是否是好评单
     * @return
     */
    @RequestMapping(value = "calculateIsFavour",method = RequestMethod.POST)
    @ApiOperation(value = "计算是否是好评单",notes = "development by yinyuxin")
    public ResultData<Boolean> calculateIsFavour(FavourTargetDto favourTargetDto){
        LOGGER.info("微服务 driverIntegration-->calculateIsFavour()-->param:{}",BeanJsonUtil.obj2String(favourTargetDto));
        try {
            ResultData<Boolean> resultData=new ResultData<>();
            if (StringUtil.isNotEmpty(favourTargetDto.getSignTime()) && StringUtil.isNotEmpty(favourTargetDto.getEvaluationTime())){
                Date signDate=DateUtil.string2Date(favourTargetDto.getSignTime(),DateUtil.FROMAT_DAtE);
                Date evaluationTime=DateUtil.string2Date(favourTargetDto.getEvaluationTime(),DateUtil.FROMAT_DAtE);
                if (DateUtil.diffDay(signDate,evaluationTime)>7){
                    resultData.setData(true);
                    return resultData;
                }
            }
            //计算星数
            double favourDecimal=favourTargetDto.getGoodPackageStar()*0.1+favourTargetDto.getWlSpeedStar()*0.4+favourTargetDto.getWlServiceStar()*0.5;
            if (favourDecimal>=4){
                resultData.setData(true);
            }else{
                resultData.setData(false);
            }
            return resultData;
        } catch (Exception e) {
            return new ResultData<>(ErrorCode.FAILOR,ErrorCode.FAILOR_MSG);
        }
    }

}
